package com.coco.homework.convert;

import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Arrays;

/**
 * 解析用户的输入
 * @author yanphu
 *
 */
public class InputParse {

	private Inputs inputs;

	private Object[] objs;

	public InputParse(Inputs inputs,Object[] objs){
		this.inputs = inputs;
		this.objs = objs;
	}
	
	public Inputs getInputs() {
		return inputs;
	}

	public Object[] getObjs() {
		return objs;
	}

	@Override
	public String toString() {
		return "InputParse [inputs=" + inputs + ", objs=" + Arrays.toString(objs) + "]";
	}
	
	public static InputParse parse(String input) {
		InputParse parse;
		for (Inputs in : Inputs.values()) {
			try {
				Object[] objs = new MessageFormat(in.input).parse(input);
				parse = new InputParse(in,objs);
				return parse;
			} catch (ParseException e) {

			}
		}
		parse = new InputParse(Inputs.UNKONWS_INPUT,null);
		return parse;
	}

	public static enum Inputs {
		HOW_MUCH_INPUT("how much is {0} ?", "{0} is {1}"), 
		HOW_MANY_INPUT("how many Credits is {0} ?",	"{0} is {1} Credits"), 
		EXEC_INPUT("{0} is {1} Credits",""),
		IS_INPUT("{0} is {1}", ""), // 放在HOW_MUCH_INPUT 和 HOW_MANY_INPUT 之后
		UNKONWS_INPUT("{0}", "I have no idea what you are talking about");

		private String input;

		private String output;

		Inputs(String input, String output) {
			this.input = input;
			this.output = output;
		}

		public String getInput() {
			return input;
		}

		public String getOutput() {
			return output;
		}
	}
}
